<?php
include_once('Es.class.php');
include_once('functions.php');

$tmpResult =  EsGetList();
echo $tmpResult;

function EsGetList(){
	global $wpdb;
	$result = array();
	$result['status'] = 0;
	$result['info'] = "Fileserving Magic Sync Page";
	
	if($_GET['key']){
		$api_key = get_option('es_api_key');
		if($api_key == $_GET['key']){
			//1. 
			$sql = "SELECT COUNT(*) AS total_count FROM ".$wpdb->prefix."users ";
			$total_count = $wpdb->get_results($sql);
			$result['total_rows_count'] = $total_count[0]->total_count;
			
			$sql = "SELECT * FROM ".$wpdb->prefix."users ORDER BY id DESC LIMIT 1";
			$last_row = $wpdb->get_results($sql);
			$result['last_row_id'] = $last_row[0]->ID;
			
			//2. create
			if(!empty($_GET['create'])){
				$cmid = empty($_GET['cmid'])?0:$_GET['cmid']; //cmid stand for 'create minimum id'
				$climit = empty($_GET['climit'])?0:$_GET['climit']; // climit stand for 'create limit'
				if(!empty($cmid)&&!empty($climit)){
					$sql = "SELECT id, user_email AS email_address, user_nicename AS first_name, user_login AS last_name  FROM ".$wpdb->prefix."users WHERE id>=$cmid LIMIT $climit";
					$result['create']['data'] = $wpdb->get_results($sql);
					if(!empty($result['create']['data'])){
						$result['status'] = 1;
						$count = count($result['create']['data']);
						$result['create']['count'] = $count;
					}
				}
			}
		}
		else{
			echo 'key error!';
		}
	}
	
	return json_encode($result);
}


